//
//  seqencingViewController.m
//  基本框架
//
//  Created by runone02 on 2018/8/17.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "seqencingViewController.h"
#import "ChineseString.h"

@interface seqencingViewController ()<UITableViewDelegate,UITableViewDataSource>
/** */
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *indexArray;
@property (nonatomic, strong) NSMutableArray *letterResultArr;
@property (nonatomic, strong) UILabel *sectionLabel;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation seqencingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadtableView];
    [self setupNavView];
}

- (void)setupNavView{
    [self.navView.leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.navView.centerButton setTitle:@"通讯录" forState:UIControlStateNormal];
}



- (void)loadtableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kTopBarHeight, ScreenW, ScreenH - 65) style:UITableViewStylePlain];
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:self.tableView];
    //右侧索引颜色
    self.sectionLabel = ({
        UILabel *sectionLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2, (SCREEN_HEIGHT-100)/2,100,100)];
        sectionLabel.textAlignment = NSTextAlignmentCenter;
        sectionLabel.font = [UIFont boldSystemFontOfSize:60];
        sectionLabel.textColor = [UIColor blueColor];
        sectionLabel.backgroundColor = [UIColor clearColor];
        sectionLabel.layer.cornerRadius = 6;
        sectionLabel.layer.borderWidth = 1.f/[UIScreen mainScreen].scale;
        _sectionLabel.layer.borderColor = [UIColor clearColor].CGColor;
        sectionLabel;
    });
    [self.navigationController.view addSubview:self.sectionLabel];
    self.sectionLabel.hidden = YES;

    NSArray *stringsToSort = [NSArray arrayWithObjects:
                              @"￥hhh, .$",@" ￥Chin ese ",@"开源中国 ",@"www.oschina.net",
                              @"开源技术",@"社区",@"开发者",@"传播",
                              @"2014",@"a1",@"100",@"中国",@"暑假作业",
                              @"键盘", @"鼠标",@"hello",@"world",@"b1",@"🐂",@"凯几",@"几开",@"几开",@"咯牛",@"熟背",@"但是",@"费",@"破刹车",@"全额",@"主线",@"目标",@"不错",@"相册",
                              nil];
    
    self.indexArray = [ChineseString IndexArray:stringsToSort];
    self.letterResultArr = [ChineseString LetterSortArray:stringsToSort];

}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.indexArray;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *key = [self.indexArray objectAtIndex:section];
    return key;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [self.indexArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[self.letterResultArr objectAtIndex:section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    cell.textLabel.text = [[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    [self showsectionLabel:title];
    return index;
    
}

#pragma mark - private
- (void)timerHandler:(NSTimer *)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:.1 animations:^{
            self.sectionLabel.alpha = 0;
        } completion:^(BOOL finished) {
            self.sectionLabel.hidden = YES;
        }];
    });
}

- (void)showsectionLabel:(NSString*)title{
    [self.sectionLabel setText:title];
    self.sectionLabel.hidden = NO;
    self.sectionLabel.alpha = 1;
    [self.timer invalidate];
    self.timer = nil;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerHandler:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

#pragma mark -
#pragma mark - UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lab = [UILabel new];
    lab.backgroundColor = [UIColor groupTableViewBackgroundColor];
    lab.text = [self.indexArray objectAtIndex:section];
    lab.textColor = [UIColor blackColor];
    return lab;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"---->%@",[[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]);
//
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:[[self.letterResultArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]
                                                   delegate:nil
                                          cancelButtonTitle:@"YES" otherButtonTitles:nil];
    [alert show];
}

@end
